package com.rihel.util;

import com.rihel.model.Goods;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class CartHelper {
    private HashMap<Goods,Integer> carts =null;
    private double taotalPrice ;
    public CartHelper(){
        this.carts = new HashMap<Goods, Integer>();
        this.taotalPrice=0.0;
    }



    public boolean addGoodsToCart(Goods goods,Integer count){
        if(goods!=null){
            this.carts.put(goods,count);
            this.calTotalPrice();
            return true;
        }else{
            return false;
        }
    }

    public boolean removeGoodsInCart(Goods goods){
        if(goods!=null){
            this.carts.remove(goods);
            this.calTotalPrice();
            return true;
        }else{
            return false;
        }
    }


    public double calTotalPrice(){
        double price = 0.0 ;
        Set<Goods> goodsSet = this.carts.keySet();
        for (Goods goods:goodsSet) {
            price+= goods.getPrice() * this.carts.get(goods);
        }
        this.setTaotalPrice(price);
        return price;
    }

    public HashMap<Goods, Integer> getCarts() {
        return carts;
    }

    public void setCarts(HashMap<Goods, Integer> carts) {
        this.carts = carts;
    }

    public double getTaotalPrice() {
        return taotalPrice;
    }

    public void setTaotalPrice(double taotalPrice) {
        this.taotalPrice = taotalPrice;
    }
}
